﻿using ERPSystem.Common.Domains.CustomerProfileCommon;
using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SalesManagementWrite.API.SalesManagementWrites.Deliverynotice;
using SalesManagementWrite.API.SalesManagementWrites.PurchaseInvoicees;
using SalesManagementWrite.API.SalesManagementWrites.PurchaseReturnOrderes;
using SalesManagementWrite.API.SalesManagementWrites.SalesOrders;
using Yitter.IdGenerator;
using System.Globalization;

namespace SalesManagementWrite.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SalesManagementWriteController : ControllerBase
    {
        private readonly IMediator _mediator;

        public SalesManagementWriteController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 销售订单新增接口
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> CreateSalesOrder(SalesOrderCreateCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 处理创建销售订单物料命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> CreateSalesOrderMaterial(SalesOrderMaterialCreateCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 发货通知单新增接口
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        [HttpPost]
        public async Task<Result> DeliverynoticeCreateHandle(DeliverynoticeCreateCommon request)
        {
            //http://localhost:5020/api/SalesManagementWrite/Delive rynoticeCreateHandle
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 新增采购发票
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PurchaseInvoiceCreate(PurchaseInvoiceCreateCommand request, CancellationToken cancellationToken)
        {
            request.SerialNumber = Yitter.IdGenerator.YitIdHelper.NextId().ToString();
            request.DocumentNo = "CGFP" + YitIdHelper.NextId().ToString();
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public string UploadFile(IFormFile file)
        {
            var fileName = file.FileName;
            var path = Directory.GetCurrentDirectory() + @"\\wwwroot\\Image\\" + fileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var url = "/Image/" + fileName;
            return url;
        }

        /// <summary>
        /// 导入发货通知单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ImportHandle(ImportDeliveCommand request)
        {
            var result = await _mediator.Send(request);
            return result;

        }
        /// <summary>
        /// 修改发货通知单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<Result> ShippingnoteUpdateHandle(ShippingnoteUpdateCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);


        }

        /// <summary>
        /// 采购退货单新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PurchaseReturnOrderCreate(PurchaseReturnOrderCreateCommand request, CancellationToken cancellationToken)
        {
            request.PurchaseReturnOrderSerialNumber = YitIdHelper.NextId().ToString();//流水号
            request.PurchaseReturnOrderNo = "CGTH" + YitIdHelper.NextId().ToString();//单据号
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 修改采购退货单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> PurchaseReturnOrderUpdate(PurchaseReturnOrderUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
    }
}
